<?php
define("__ALIPATH__",dirname(__FILE__).'/aliopen');           //定义根目录
use app\common\model\SystemConfig as CONFIG;

include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/ExampleFacade.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyGetParam.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyPostParam.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyGetResult.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyPostResult.class.php');

include_once (__ALIPATH__.'/com/alibaba/openapi/client/entity/ByteArray.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/util/DateUtil.class.php');

class Aliopen
{	
	private $webSite=1688;
	public $imageHost='https://cbu01.alicdn.com/';
    private $productExample;
	private $dealExample;
	private $conf;

    public function __construct($params=[])
    {
        $this->conf=CONFIG::get('api.alibaba');
        $this->setProductConf();
		$this->setDealConf();
    }
    
	// 实例化商品模块
	private function setProductConf(){
		$this->productExample = new \ExampleFacade();
		$this->productExample->setAppKey($this->conf['product_app_key']);
		$this->productExample->setSecKey($this->conf['product_app_secret']);
		$this->productExample->setToken($this->conf['product_token']);
	}
	
	// 实例化订单模块
	private function setDealConf(){
		$this->dealExample = new \ExampleFacade();
		$this->dealExample->setAppKey($this->conf['deal_app_key']);
		$this->dealExample->setSecKey($this->conf['deal_app_secret']);
		$this->dealExample->setToken($this->conf['deal_token']);
	}
	
	// 过滤无用参数，输出核心参数
	public function result($data,$exist,$key=''){
		$key=$key==''?$exist:$key;
		if(isset($data[$exist])){
			return spot_key($data,$key);
		}else{
			return $data;
		}
	}
	
	//错误提示
	private function errorResult($ex){
		return ['error_code'=>$ex->getErrorCode(),'error_message'=>$ex->getMessage()];
	}
	
    public function get_product($productID = ''){
        try {
            $params=[
                'productID'=>$productID,
            ];
			$api_name='alibaba.product.get';
			$api_url='com.alibaba.product';
			$this->productExample->ApiPost($api_name,$api_url,$params,$this->productExample);
			$res=$this->productExample->getResult();
            return $res['productInfo'];
        } catch ( \OceanException $ex ) {
        	return $this->errorResult($ex);
        }
    }
	
	// 获取商品列表
	public function product_list($pageNo=1,$pageSize=20,$status=1){
		try {
			if($status==1) $status=['published'];
			$params=[
				'pageNo'=>$pageNo,
				'pageSize'=>$pageSize,
				// 'statusList'=>$status,
			];
			$api_name='alibaba.product.list.get';
			$api_url='com.alibaba.product';
			$this->productExample->ApiPost($api_name,$api_url,$params,$this->productExample);
			$res=$this->productExample->getResult();
			return $res['result']['pageResult'];
		} catch ( \OceanException $ex ) {
			return $this->errorResult($ex);
		}
	}
	
	//	修改商品
	public function product_up($id,$data){
		try {
			$params=[
				'offerId'		=>	$id,
				'dataBody'		=>	json_encode($data),
				'scene'			=>	'cbu',
			];
			$api_name='alibaba.new.product.edit';
			$api_url='com.alibaba.product';
			$this->productExample->ApiPost($api_name,$api_url,$params,$this->productExample);
			$res=$this->productExample->getResult();
			p($res);
			if($res['result']['success']){
				return $res['result'];
			}else{
				return ['error_message'=>$res['result']['bizMsg']];
			}
		} catch ( \OceanException $ex ) {
			return $this->errorResult($ex);
		}
	}
}

?>